package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by j_hu on 5/7/17.
 */

public abstract class SyncMetaCommandRespHandler implements Command.CommandHandler {

    private String name;
    private SyncMetaCommand cmd;

    public SyncMetaCommandRespHandler(String name, SyncMetaCommand cmd) {
        this.cmd = cmd;
        this.name = name;
    }

    protected Command getCommand() {
        return cmd;
    }

    protected String getString(InputStream input) {
        String result = null;
        try {
            result = Util.readAll(input);
        } catch (IOException e) {
            cmd.raiseError(e);
        }
        return result;


//        String result = null;
//        InputStreamReader in = null;
//        try {
//            String line;
//            in = new InputStreamReader(input);
//            StringBuffer sb = new StringBuffer();
//            BufferedReader reader = new BufferedReader(in);
//            while ((line = reader.readLine()) != null) {
//                sb.append(line);
//            }
//            result = sb.toString();
//        }catch(Exception e) {
//            cmd.raiseError(e);
//        } finally {
//            if (in != null) {
//                try {
//                    in.close();
//                }catch(IOException e) {
//                    cmd.raiseError(e);
//                }
//            }
//        }
//        return result;
    }

    protected String getName() {
        return this.name;
    }

    public class SyncMetaCommandRS<T> {

        @SerializedName("last_meta_update_time")
        private String lastMetaUpdateTime;

        private T data;

        public String getLastMetaUpdateTime() {
            return lastMetaUpdateTime;
        }

        public T getData() {
            return data;
        }
    }
}
